#! /bin/sh

#解锁keychain 此命令行设置jenkins的时候才要调用 -p代表password 按照打包机器设置密码即可（替换ssd）
#security unlock-keychain -p ssd ~/Library/Keychains/login.keychain

#获取父目录
fadir()
{
local this_dir=`pwd`
local child_dir="$1"
dirname "$child_dir"
cd $this_dir
}

script_Path=$(cd "$(dirname "$0")"; pwd)
archive_Path=`fadir $script_Path`

param_pattern="aitsfh"
OPTIND=1
while getopts $param_pattern optname
do
case "$optname" in
"a")
should_build_AdHoc=y
;;
"i")
should_build_InHouse=y
;;
"t")
should_build_International=y
;;
"s")
should_build_Simulators=y
;;
"f")
should_upload_fir=y
;;
"h")
echo "options\n \
      -a build AdHoc\n \
      -i build Enterprise InHouse \n \
      -t build International AdHoc \n \
      -s build Simulators \n \
      -f upload to fir.im"
exit 2
;;
*)
echo "should_build_AdHoc"
exit 2
;;
esac
done

if [ "$should_build_AdHoc" != "y" -a "$should_build_InHouse" != "y" -a "$should_build_International" != "y" -a "$should_build_Simulators" != "y"  ];then
    should_build_AdHoc=y
fi

if test -f $archive_Path"/buildSync.sh"; then
    sh $archive_Path"/buildSync.sh"
fi

project_name="MSJD"
project_target="MSJD"
project_path="${archive_Path}"

rm -rf "${archive_Path}/build"

#check AdHoc dir
if [ "$should_build_AdHoc" == "y" ];then
    echo "need build AdHoc"
    archive_path_AdHoc="${archive_Path}/build/adHoc"
    echo "create package_path ${archive_path_AdHoc}"
    if [ ! -d "${archive_path_AdHoc}" ]; then
    mkdir -p "${archive_path_AdHoc}"
    fi
fi

#check InHouse dir
if [ "$should_build_InHouse" == "y" ];then
    echo "need build InHouse"
    archive_path_InHouse="${archive_Path}/build/inHouse"
    echo "create package_path ${archive_path_InHouse}"
    if [ ! -d "${archive_path_InHouse}" ]; then
    mkdir -p "${archive_path_InHouse}"
    fi
fi

#check International dir
if [ "$should_build_International" == "y" ];then
    echo "need build International"
    archive_path_International="${archive_Path}/build/International"
    echo "create package_path ${archive_path_International}"
    if [ ! -d "${archive_path_International}" ]; then
    mkdir -p "${archive_path_International}"
    fi
fi

#check Simulators dir
if [ "$should_build_Simulators" == "y" ];then
    echo "need build Simulators"
    archive_path_Simulators="${archive_Path}/build/simulators"
    echo "create package_path ${archive_path_Simulators}"
    if [ ! -d "${archive_path_Simulators}" ]; then
    mkdir -p "${archive_path_Simulators}"
    fi
fi


#fir token

#平台运营中心token ----------------
#firToken="6142a6ee4ec89f4788b41c50cbf1693e"
#firToken="23133a2e2e52403f1e3a8e93e359983a"
firToken="fded7cddd96411041289b6ba13748e41"
#firToken="feb3fdf31bc9d9f045275d1f69e92f87"
#firToken="5a8dd53c4c98887518cbb91835d59937"

configuration="Release"
archivePath="${archive_path_AdHoc}"
plistName="msjd"

if [ "$should_build_AdHoc" == "y" -o "$should_build_InHouse" == "y" -o "$should_build_International" == "y"  ];then
    echo "xcodebuild clean -sdk iphoneos -scheme $project_target -configuration $configuration"
    xcodebuild clean -project ${project_path}/${project_name}.xcworkspace -sdk iphoneos -scheme $project_target -configuration "${configuration}"
    echo "xcodebuild archive $configuration"
    xcodebuild archive -hideShellScriptEnvironment -workspace ${project_path}/${project_name}.xcworkspace -archivePath "${archivePath}/${project_target}.xcarchive" -sdk iphoneos -scheme "$project_target" -configuration "${configuration}"
    echo "xcodebuild exportArchive ${configuration}"
    xcodebuild -project ${project_path}/${project_name}.xcworkspace -exportArchive -archivePath "${archivePath}/${project_target}.xcarchive" -exportPath "${archivePath}/" -exportOptionsPlist "${script_Path}/${plistName}.plist"
    if [ "$should_upload_fir" == "y" ];then
    echo "upload fir..."
    "fir" p ${archivePath}/$project_target.ipa -T $firToken
    fi
fi

#build Simulators
if [ "$should_build_Simulators" == "y" ];then
	echo "xcodebuild clean -sdk iphonesimulator -scheme $project_target -configuration Release"
	#xcodebuild clean -project ${project_path}/${project_name}.xcodeproj -sdk iphonesimulator -scheme $project_target -configuration Release
	echo "xcodebuild Simulators"
	xcodebuild -hideShellScriptEnvironment -project ${project_path}/${project_name}.xcodeproj -sdk iphonesimulator -scheme "$project_target" -configuration "Release"  -arch x86_64 CONFIGURATION_BUILD_DIR="./../${archive_path_Simulators}/${project_target}"
fi
